let form = document.querySelector('.form-wrap');
let username = form.children[1].children[1];
let password = form.children[2].children[1];
let reset = form.children[4].children[0];
let submit = reset.nextElementSibling;
let mask = document.getElementById('mask');
reset.onclick = function() {
	username.value = '';
	password.value = '';
}
submit.onclick = function() {
	let errorTip = form.children[0];
	if(username.value.length == 0 || password.value.length == 0) {
		errorTip.style.display = 'block';
		errorTip.innerText = '用户名或密码不能为空';
	} else {
		ajax({
			type: 'POST',
			url: '/admin/login',
			async: true,
			data: {
				username: username.value.trim(),
				password: hex_md5(password.value.trim()),
				number: form.children[3].value
			},
			beforeSend: function() {
				errorTip.style.display = 'none';
				// 显示等待图片
				mask.style.display = 'block';
				mask.innerHTML = `<div class="mask-wrap" style="position: absolute; top: 50%; left: 50%; margin-top: -45px;margin-left: -45px;width: 90px;height: 90px;background: url(/images/loading2.gif) no-repeat;background-size: 90px;">
								  </div>`;
			},
			success: function(xhr) {
				removeAllChildren(mask);
				mask.style.display = 'none';
				let arg = JSON.parse(xhr.responseText);
				if(arg.code == 200) {
					location.href = '/page/admin/index.jsp?' + new Date().getTime();
				} else if(arg.code == 3010){
					errorTip.innerText = '用户名或密码不正确';
					errorTip.style.display = 'block';
				} else if(Arg.code == 300) {
					errorTip.innerText = '用户名或密码不能为空';
					errorTip.style.display = 'block';
				}
			}
		});
	}
	
}